package org.example.wx.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.example.common.utils.DateTimeUtil;
import org.example.db.entity.FootPrint;
import org.example.db.mapper.FootPrintMapper;
import org.example.security.service.AuthService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxFootPrintService {


    FootPrintMapper footPrintMapper;
    AuthService authService;

    public WxFootPrintService(FootPrintMapper footPrintMapper, AuthService authService) {
        this.footPrintMapper = footPrintMapper;
        this.authService = authService;
    }

    public List<FootPrint> list(){

        return footPrintMapper.selectList(Wrappers.lambdaQuery(FootPrint.class)
                .eq(FootPrint::getUserId, authService.getWxUserId())
                .last("limit 10"));
    }

    public void add(Long goodsId){



        Long wxUserId = authService.getWxUserId();
        List<FootPrint> footPrints = footPrintMapper.selectList(Wrappers.lambdaQuery(FootPrint.class).eq(FootPrint::getUserId, wxUserId).eq(FootPrint::getGoodsId, goodsId));

        if(footPrints.isEmpty()){
            FootPrint footPrint = new FootPrint();
            footPrint.setUserId(wxUserId);
            footPrint.setGoodsId(goodsId);
            footPrint.setUpdateTime(DateTimeUtil.getDateTimeString());
            footPrint.setAddTime(DateTimeUtil.getDateTimeString());
            footPrintMapper.insert(footPrint);
        }else{
            footPrintMapper.update(null, Wrappers.lambdaUpdate(FootPrint.class)
                    .eq(FootPrint::getId, footPrints.get(0).getId()).set(FootPrint::getUpdateTime, DateTimeUtil.getDateTimeString()));
        }



    }
}
